package com.chap1;

/**
 * <p>
 * <code>HasInterruptException</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-03-20 15:05
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class HasInterruptException {
    public static void main(String[] args) throws InterruptedException {
        Thread endThread = new UseThread("HasInterrputEx");
        endThread.start();
        Thread.sleep(500);
        endThread.interrupt();
    }

    private static class UseThread extends Thread{
        public UseThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            while (!isInterrupted()){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.out.println(Thread.currentThread().getName()
                            +" in InterruptedException interrupt flag is "
                            +isInterrupted());
                    interrupt();//设置中断标志位=true  跳出循环
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()
                        + " I am extends Thread.");
            }
            //while 循环结束后才会打印本句
            System.out.println(Thread.currentThread().getName()
                    +" interrupt flag is "+isInterrupted());
        }
    }
}
